
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
    public class HTTPClients {
        private Socket socket;
        private String ip;
        //定义窗口号
        private int port;
        //构造方法赋值
        public HTTPClients(String ip,int port) throws IOException {
            this.ip=ip;
            this.port=port;
            socket=new Socket(ip,port);
        }
        //get方法
        public String get(String url) throws IOException {
            StringBuilder request=new StringBuilder();
            request.append("GET"+url+"HTTP/1.1\n");
            //构造 header
            request.append("Host: "+ip+" "+port+"\n");
            //构造 空行
            request.append("\n");
            //发送数据
            OutputStream outputStream=socket.getOutputStream();
            outputStream.write(request.toString().getBytes());//利用字节去写入
            //读取响应数据
            InputStream inputStream= socket.getInputStream();
            byte[] buffer=new byte[1024*1024];
            int n=inputStream.read(buffer);
            return  new String(buffer,0,n,"UTF-8");
        }
    }